<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="value"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      :disabled="!value"
      class="post-btn"
      @click="onPost"
    >发布</van-button>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { PostComment } from '@/api/article.js'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: {
      type: [String, Object, Number],
      required: true
    },
    // 没有artId，就是需要对文章评论
    artId: {
      type: [String, Object, Number],
      default: null
    }
  },
  data () {
    return {
      value: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 发布评论
    async onPost () {
      // loading
      Toast.loading({
        message: '发布中...',
        duration: 0,
        forbidClick: true
      })
      try {
        const res = await PostComment({
          target: this.target,
          content: this.value,
          art_id: this.artId
        })
        console.log(res)
        Toast.success('评论成功！')
        this.$emit('on-success', res.new_obj)
        this.value = ''
      } catch (err) {
        Toast.fail('评论失败！')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
